package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.core.domain.dto.OrganizationAssignmentQuery;
import com.smedi.ismedi.inmail.core.domain.dto.OrganizationQuery;
import com.smedi.ismedi.inmail.core.domain.dto.PersonQuery;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.OrganizationRemoteApiService;
import com.smedi.ismedi.organization.api.dto.request.OrganizationListQueryRequest;
import com.smedi.ismedi.organization.api.dto.response.HrOrganizationResponse;
import com.smedi.ismedi.organization.api.dto.response.ProjectOrganizationResponse;
import com.smedi.ismedi.organization.api.dto.response.resp.OrganizationResp;
import com.smedi.ismedi.organization.api.feign.RemoteHrOrganizationFeignApi;
import com.smedi.ismedi.organization.api.feign.RemoteProjectOrganizationFeignApi;
import com.smedi.ismedi.personnel.api.dto.request.OrganizationAssignmentListQueryRequest;
import com.smedi.ismedi.personnel.api.dto.response.resp.OrganizationAssignmentResp;
import com.smedi.ismedi.personnel.api.dto.response.resp.PersonResp;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 远程组织接口实现类
 *
 * @author jiangqinxuan
 * @date 2021/07/27
 */
@Service
@AllArgsConstructor
public class OrganizationRemoteApiServiceImpl implements OrganizationRemoteApiService {
    private final RemoteProjectOrganizationFeignApi remoteProjectOrganizationFeignApi;
    private final RemoteHrOrganizationFeignApi remoteHrOrganizationFeignApi;

    private final AdapterConverter adapterConverter;

    @Override
    public List<Long> queryProjectIdListByProjectNameList(List<String> projectNameList) {
        List<Long> projectIdList = new ArrayList<>();
        List<ProjectOrganizationResponse> projectOrganizationResponses = remoteProjectOrganizationFeignApi.getQueryProjectOrganizationListByFullNameList(projectNameList).getData();
        for (ProjectOrganizationResponse projectOrganizationResponse : projectOrganizationResponses) {
            Long projectIdTemp = projectOrganizationResponse.getProjectId();
            projectIdList.add(projectIdTemp);
        }
        return projectIdList;
    }

    @Override
    public List<Long> queryOrganizationIdListByOrganizationNameList(List<String> organizationNameList) {
        List<Long> organizationIdList = new ArrayList<>();
        List<HrOrganizationResponse> hrOrganizationResponses=remoteHrOrganizationFeignApi.getQueryHrOrganizationListByFullNameList(organizationNameList).getData();
        for (HrOrganizationResponse hrOrganizationResponse : hrOrganizationResponses) {
            Long organizationIdTemp = hrOrganizationResponse.getHrOrganizationId();
            organizationIdList.add(organizationIdTemp);
        }
        return organizationIdList;

    }

    @Override
    public List<OrganizationResp> getOrganizationList(OrganizationQuery organizationQuery) {
        OrganizationListQueryRequest organizationListQueryRequest = adapterConverter.organizationQueryToOrganizationListQueryRequest(organizationQuery);
        ResponseData<List<OrganizationResp>> responseData = remoteHrOrganizationFeignApi.getOrganizationList(organizationListQueryRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return responseData.getData();
    }
}
